﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public interface IActivityLotteryControl
    {
        void GetActivityList();
        void GetItemmList(string activityID);
        void GetUsersList(string activityID, string strItemIDs, int MaxNumber);
        void GetUserInfo(string strUserID);
        void ShowDetail(string strActivityID,string strUserID, string strUserName);
        void DeleteLotteries(string LotteriesID);
        void GetActivityLotteryList(string activityID);
        void AddActivityLotteries(string activityID, string userID, int level);
    }
    public class ActivityLotteryControl : IActivityLotteryControl
    {
        IActivityService activityService;
        IAuserService usersService;
        ILotteriesService lotteriesService;
        IActivityLotteryView view;
        IItemService itemmService;
        ITickService ticketService;    

        public ActivityLotteryControl(IActivityLotteryView view)
        {
            this.view = view;
            activityService = Factory.GetInstance<IActivityService>();
            usersService = Factory.GetInstance<IAuserService>();
            lotteriesService = Factory.GetInstance<ILotteriesService>();
            itemmService = Factory.GetInstance<IItemService>();
            ticketService = Factory.GetInstance<ITickService>();
             
        }
        public void AddActivityLotteries(string activityID, string userID, int level)
        {
            string userEmail = "";
            string ticketID = "";
            VT_Tickets v_ticket = usersService.GetUersTickets(userID, activityID);
            if (v_ticket != null) ticketID = v_ticket.Ticket_ID;
            VT_Users v_User = usersService.GetUserByID(userID);
            if (v_User != null) userEmail = v_User.User_Email;
            view.AddActivityLotteriesComplete(lotteriesService.AddActivityLotteries(activityID, ticketID, userEmail, level));
        }
        public void GetActivityList()
        {
            view.GetActivityListComplete(activityService.GetActivities());
        }
        public void GetActivityLotteryList(string activityID)
        {
            view.GetActivityLotteryComplete(lotteriesService.GetActivityLotterysList(activityID));
        }
        public void DeleteLotteries(string LotteriesID)
        {
            view.DeleteLotteriesComplete(lotteriesService.DeleteLotteries(LotteriesID));
        }
    
        public void GetItemmList(string activityID)
        {
            view.GetItemmListComplete(itemmService.GetLastActiviteItemms(activityID));
        }

        public void GetUserInfo(string strUserID)
        {
            view.GetUserInfoComplete(usersService.GetUserByID(strUserID));
        }

        public void ShowDetail(string strActivityID,string strUserID, string strUserName)
        {
            string ticketContent = ticketService.GetContent(strActivityID, strUserID);
            string[] strItem = new string[ticketContent.Split('&').Length];
            int i = 0;
            foreach (string ticketGroup in ticketContent.Split('&'))
            {
                string itemId = ticketGroup.Split('=')[1];
                VT_Items v_item = itemmService.GetItems(itemId);
                strItem[i] = v_item.Item_Title;
                i++;
            }
            view.ShowDetailComplete(strItem, strUserName);
        }

        public void GetUsersList(string activityID,string strItemIDs,int MaxNumber)
        {
            IList<VT_Tickets> v_Tickets = ticketService.GetTicketsNoLottery(activityID, 1);
            string[] strTicekets = null;
            int[] iTicektCount = null;
            string[] strUsersID = null;
            //找出每个票所匹配的数量
            if (v_Tickets != null)
            {
                strTicekets = new string[v_Tickets.Count];
                iTicektCount = new int[v_Tickets.Count];
                strUsersID = new string[v_Tickets.Count];
                int iNumber = 0;
                foreach (VT_Tickets v_Ticket in v_Tickets)
                {
                    int MatchCount = 0;
                    strTicekets[iNumber] = v_Ticket.Ticket_ID;
                    strUsersID[iNumber] = v_Ticket.User_ID;
                    string ticketContent = v_Ticket.Ticket_Content;
                    foreach (string ticketGroup in ticketContent.Split('&'))
                    {
                        string itemId = ticketGroup.Split('=')[1];
                        if (strItemIDs.Contains(itemId)) MatchCount = MatchCount + 1;
                    }
                    iTicektCount[iNumber] = MatchCount;

                    iNumber++;
                }
            }

            //排序 冒泡排序
            bool isExchanged = false;

            for (int i = 0; i < iTicektCount.Length; i++)
            {
                isExchanged = false;
                for (int j = iTicektCount.Length - 1; j > i; j--)
                {
                    if (iTicektCount[j] > iTicektCount[j - 1])
                    {
                        //交换数量
                        int temp = iTicektCount[j];
                        iTicektCount[j] = iTicektCount[j - 1];
                        iTicektCount[j - 1] = temp;

                        //交换ticketID
                        string strTemp = strTicekets[j];
                        strTicekets[j] = strTicekets[j - 1];
                        strTicekets[j - 1] = strTemp;

                        //交换userID
                        strTemp = strUsersID[j];
                        strUsersID[j] = strUsersID[j - 1];
                        strUsersID[j - 1] = strTemp;

                        isExchanged = true;
                    }
                }

                if (!isExchanged)  
                    break; 　　  
            }
         
            //排序完后，可以找出前几名匹配最多的用户
            int iEndNumber = iTicektCount.Length - 1;
            if (iTicektCount.Length > MaxNumber)
            {
                iEndNumber = MaxNumber - 1;
                //找出最后一名匹配的，然后看是否有并列的票数
                int iFind = iTicektCount[MaxNumber - 1];
                for (int i = MaxNumber; i < iTicektCount.Length; i++)
                {
                    if (iFind > iTicektCount[i])
                    {
                        iEndNumber = i;
                        break;
                    }
                }
            }       
            view.GetUsersListComplete(strUsersID, strTicekets, iTicektCount, iEndNumber);
        }
    }  
}
